package com.r2coding.article.controller;

import com.r2coding.article.annotation.SysLog;
import com.r2coding.article.vo.req.ArticleReqVo;
import com.r2coding.article.vo.req.UpdateArticleReqVo;
import com.r2coding.article.vo.resp.CategoryAndLabelsRespVo;
import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.article.service.ArticleService;

/**
 * 文章信息控制器
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-12 23:41:38
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 根据文章id获取文章的标签和分类
     */
    @GetMapping("/getCategoryAndLabelsByArticleId/{id}")
    public Result getCategoryAndLabelByArticleId(@PathVariable("id") Integer id) {
        CategoryAndLabelsRespVo vo = articleService.getCategoryAndLabelByArticleId(id);
        return Result.ok(vo);
    }

    /**
     * 保存
     */
    @SysLog(module = Module.ARTICLE, operationType = OperationType.INSERT, description = "添加博客")
    @PreAuthorize("hasAuthority('blog:add')")
    @RequestMapping("/save")
    public Result save(@RequestBody ArticleReqVo vo){
		articleService.saveArticle(vo);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.ARTICLE, operationType = OperationType.UPDATE, description = "修改博客")
    @PreAuthorize("hasAuthority('blog:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody UpdateArticleReqVo vo){
        articleService.updateArticle(vo);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.ARTICLE, operationType = OperationType.DELETE, description = "删除博客")
    @PreAuthorize("hasAuthority('blog:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        articleService.deleteArticle(id);
        return Result.ok();
    }

    /**
     * 发布
     *
     * @param id 文章id
     * @return 统一响应体
     */
    @PutMapping("/release/{id}")
    public Result release(@PathVariable("id") Integer id) {
        articleService.release(id);
        return Result.ok();
    }

}
